package com.hnkywl.sts.dao.kcgl;

import com.hnkywl.sts.entity.xsgl.Kcpd;
import com.hnkywl.sts.entity.xsgl.Kh;
import com.hnkywl.sts.entity.xsgl.Pdmx;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.hibernate.type.Type;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-5-25
 * Time: 下午2:37
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class KcpdDao extends SimpleHibernateTemplate<Kcpd, Long> {
    public Page<Kcpd> findKcpdList(Long ckId, String ckMc,int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Kcpd> page = new Page<Kcpd>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (ckId != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" ck.id = ? ");
            param.add(ckId);
        }
        if (ckMc != null && !"".equals(ckMc)) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" ck.mc like ? ");
            param.add("%" + ckMc.trim() + "%");
        }
        //==============记录数量===============
        hql = "select count(*) from Kcpd where 1=1 ";
        // + RegionDao.createDataRegion("and")
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Kcpd where 1=1 " ;
        if (condition.length() != 0)
            hql = hql + " and " + condition + "order by dateCreated desc";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }

    public List<Map> findPdmxList(Long kcpdId) {
        Criteria criteria = getSession().createCriteria(Pdmx.class)
                .createAlias("kh", "kh", CriteriaSpecification.LEFT_JOIN)
                .createAlias("kh.jtwz", "wzgg", CriteriaSpecification.LEFT_JOIN)
                .createAlias("kh.cd", "cd", CriteriaSpecification.LEFT_JOIN)
                .setProjection(Projections.projectionList()
                        .add(Property.forName("id").as("id"))
                        .add(Property.forName("spsl").as("spsl"))
                        .add(Property.forName("yksl").as("yksl"))
                        .add(Property.forName("ykje").as("ykje"))
                        .add(Property.forName("kh.khbm").as("kh.khbm"))
                        .add(Property.forName("wzgg.id").as("wzgg.id"))
                        .add(Property.forName("wzgg.code").as("wzgg.code"))
                        .add(Property.forName("wzgg.name").as("wzgg.name"))
                        .add(Projections.sqlProjection("wzgg2_.cz||' '||wzgg2_.ggxh as \"wzgg.ggxh\"", new String[]{"wzgg.ggxh"}, new Type[]{Hibernate.STRING}))
                        .add(Property.forName("wzgg.ggxh").as("wzgg.ggxh"))
                        .add(Property.forName("wzgg.cz").as("wzgg.cz"))
                        .add(Property.forName("wzgg.dw").as("wzgg.dw"))
                        .add(Property.forName("cd.id").as("cd.id"))
                        .add(Property.forName("cd.z").as("cd.z"))
                        .add(Property.forName("zmsl").as("zmsl"))
                        .add(Property.forName("wsdj").as("wsdj"))
                ).add(Restrictions.eq("kcpd.id",kcpdId)).addOrder(Property.forName("wzgg.code").asc())
                .setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
        return (List<Map>) criteria.list();
    }

    public List<Map> findPdmxByKh(Kh kh) {
        Criteria criteria = getSession().createCriteria(Pdmx.class)
                .createAlias("kh", "kh", CriteriaSpecification.LEFT_JOIN)
                .createAlias("kh.jtwz", "wzgg", CriteriaSpecification.LEFT_JOIN)
                .createAlias("kh.cd", "cd", CriteriaSpecification.LEFT_JOIN)
                .setProjection(Projections.projectionList()
                        .add(Property.forName("id").as("id"))
                        .add(Property.forName("spsl").as("spsl"))
                        .add(Property.forName("yksl").as("yksl"))
                        .add(Property.forName("ykje").as("ykje"))
                        .add(Property.forName("kh.khbm").as("kh.khbm"))
                        .add(Property.forName("wzgg.id").as("wzgg.id"))
                        .add(Property.forName("wzgg.code").as("wzgg.code"))
                        .add(Property.forName("wzgg.name").as("wzgg.name"))
                        .add(Property.forName("wzgg.ggxh").as("wzgg.ggxh"))
                        .add(Property.forName("wzgg.dw").as("wzgg.dw"))
                        .add(Property.forName("cd.id").as("cd.id"))
                        .add(Property.forName("cd.z").as("cd.z"))
                        .add(Property.forName("kh.sl").as("kh.sl"))
                        .add(Property.forName("kh.wsdj").as("kh.wsdj"))
                ).add(Restrictions.eq("kh.id", kh.getId()))
                .setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
        return (List<Map>) criteria.list();
    }

    /**
     * 判断属性是否重复
     * @param id 库存盘点id
     * @param propertyName 库存盘点属性名
     * @param propertyValue 库存盘点属性值
     * @return
     */
    public Boolean checkUnique(Long id, String propertyName,Object propertyValue) {
        String hql;
        if (propertyValue == null)
            return false;
        if(id == null){
            //修改检验
            hql = "from Kcpd where " + propertyName + " = ? ";
            return this.existsQuery(hql, propertyValue);
        }else{
            //新增检验
            hql = "from Kcpd where " + propertyName + " = ? and id <>?";
            return this.existsQuery(hql, propertyValue,id);
        }
    }
}
